//
//  ReportVC.m
//  代理端
//
//  Created by sliver on 15/12/12.
//  Copyright (c) 2015年 sliver. All rights reserved.
//

#import "ReportVC.h"
#import "Unit.h"
#import "WXDataService.h"
#import "TypeBtn.h"
typedef NS_ENUM(NSInteger ,WitchBtnclic) {
WitchBtnclicCaguer,//商品分类
    WitchBtnclicName,//商品name
    WitchBtnclicType//商品的类型

} ;

@interface ReportVC ()<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
{
    UIButton *nameBtn; //分组
    UIButton *markBtn; //名称
    UIButton *classifyBtn; //型号
    
    NSMutableArray *nameAry;
    NSMutableArray *markAry;
    NSMutableArray *classifyAry;
    
    NSString *nameStr;
    NSString *markStr;
    NSString *classifyStr;
    
    UITextField *numTextField;
    UITableView *tableview;
    WitchBtnclic btenclic;
    
    NSString *stringmark;
    NSString *productId;
    NSString *nameid;
    
}
@property(nonatomic,strong)UITableView *typetableview;
@end

@implementation ReportVC
//-(UITableView *)typetableview
//{
//    if (!_typetableview) {
//        _typetableview = [[UITableView alloc]initWithFrame:CGRectMake(WITH_FOR_40, WITH_FOR_100+WITH_FOR_40,ScreenW-WITH_FOR_80 , 160)];
//        _typetableview.delegate = self;
//        
//        _typetableview.dataSource = self;
//    }
//    return _typetableview;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    nameAry = [[NSMutableArray alloc]init];
    markAry = [[NSMutableArray alloc]init];
    classifyAry = [[NSMutableArray alloc]init];
    
    [self loadNav];
    [self loadmainView];
    
    [self.view addSubview:self.typetableview];
    tableview = [[UITableView alloc]initWithFrame:CGRectMake(WITH_FOR_40, WITH_FOR_100+WITH_FOR_40,ScreenW-WITH_FOR_80 , 160)];
    tableview.dataSource = self;
    tableview.delegate = self;
    tableview.hidden = YES;
    [self.view addSubview:tableview];

}
//商品分类
-(void)loadNamedata{
    markStr = nil;
    classifyStr = nil;
    [MyMbd addHudToView:self.view];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [WXDataService requestWithURL:@"mobile/commodity/listCommodityCategory.jhtml" params:params httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
        [MyMbd hidenHudFromView:self.view];
        BOOL suc =[success boolValue];
        if (suc == 0 ) {
            UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [deleAlertView show];
            return;
        }else if(suc == 1){
            tableview.frame = CGRectMake(WITH_FOR_40, nameBtn.frame.origin.y+WITH_FOR_40,ScreenW-WITH_FOR_80 , 160);
            tableview.hidden= NO;
            nameAry = result[@"commodity_categories"][@"listItems"];
            btenclic =WitchBtnclicCaguer;
            [tableview reloadData];
        }
    }failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    if(![nameStr isEqualToString:@"请选择商品分类"]){
        [markBtn setTitle:@"请选择商品名称" forState:UIControlStateNormal];
        [classifyBtn setTitle:@"请选择商品类型" forState:UIControlStateNormal];
    }
}

-(void)loadmainView{
    nameBtn = [[UIButton alloc]initWithFrame:CGRectMake(WITH_FOR_40, WITH_FOR_100, ScreenW-WITH_FOR_80, WITH_FOR_40)];
    [nameBtn setTitle:@"请选择商品分类" forState:UIControlStateNormal];
    [nameBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    nameBtn.layer.masksToBounds = YES;
    nameBtn.layer.cornerRadius = 5;
    nameBtn.backgroundColor = [UIColor whiteColor];
    [nameBtn addTarget:self action:@selector(loadNamedata) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nameBtn];
    
    markBtn = [[UIButton alloc]initWithFrame:CGRectMake(WITH_FOR_40, WITH_FOR_100+WITH_FOR_50, ScreenW-WITH_FOR_80, WITH_FOR_40)];
    [markBtn setTitle:@"请选择商品名称" forState:UIControlStateNormal];
    [markBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    markBtn.layer.masksToBounds = YES;
    markBtn.layer.cornerRadius = 5;
    markBtn.backgroundColor = [UIColor whiteColor];
    [markBtn addTarget:self action:@selector(loadmarkdata) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:markBtn];
    
    classifyBtn = [[UIButton alloc]initWithFrame:CGRectMake(WITH_FOR_40, WITH_FOR_100+WITH_FOR_50+WITH_FOR_50, ScreenW-WITH_FOR_80, WITH_FOR_40)];
    [classifyBtn setTitle:@"请选择商品类型" forState:UIControlStateNormal];
    [classifyBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    classifyBtn.layer.masksToBounds = YES;
    classifyBtn.layer.cornerRadius = 5;
    classifyBtn.backgroundColor = [UIColor whiteColor];
    [classifyBtn addTarget:self action:@selector(loadclassifydata) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:classifyBtn];
    
    numTextField = [[UITextField alloc]initWithFrame:CGRectMake(WITH_FOR_40, WITH_FOR_100+WITH_FOR_50+WITH_FOR_100, ScreenW-WITH_FOR_80, WITH_FOR_40)];
    numTextField.delegate = self;
    numTextField.textColor = [UIColor grayColor];
    numTextField.font = [UIFont systemFontOfSize:17];
    numTextField.placeholder = @"请输入数量";
    numTextField.textAlignment = NSTextAlignmentCenter;
    numTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    numTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    numTextField.layer.masksToBounds = YES;
    numTextField.layer.cornerRadius = 5;
    numTextField.backgroundColor = [UIColor whiteColor];
    numTextField.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:numTextField];
    
    //确定按钮
    UIButton *landBtn = [[UIButton alloc]initWithFrame:CGRectMake(60, ScreenH - 100, ScreenW - 120, 50)];
    landBtn.backgroundColor = DRINK_RED_COLOR;
    landBtn.layer.masksToBounds = YES;
    landBtn.layer.cornerRadius = 5;
    [landBtn setTitle:@"提交库存" forState:UIControlStateNormal];
    landBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [landBtn addTarget:self action:@selector(clicktijaio) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:landBtn];
  }
#pragma mark - 提交库存
-(void)clicktijaio{
    if(nameStr ==nil || markStr==nil || classifyStr == nil){
        UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:@"请正确选择选项" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [deleAlertView show];
        return;
    }
    
    [MyMbd addHudToView:self.view];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:markStr forKey:@"commodity_name"];
    [params setObject:productId forKey:@"commodity_model_id_"];
    [params setObject:numTextField.text forKey:@"count"];
    [params setObject:USER_ACCOUNT forKey:@"agency_id_"];
    [WXDataService requestWithURL:@"mobile/agency/reportStock.jhtml" params:params httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
        [MyMbd hidenHudFromView:self.view];
        BOOL suc =[success boolValue];
        if (suc == 0 ) {
            UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [deleAlertView show];
            return;
        }else if(suc == 1){
            UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [deleAlertView show];
            return;
        }
    }failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
//产品类型
-(void)loadclassifydata{
    if(!markStr){
        UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:@"请选择商品名称" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [deleAlertView show];
        return;
    }
    
    [MyMbd addHudToView:self.view];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:stringmark forKey:@"commodity_id_"];
    

    
    [WXDataService requestWithURL:@"mobile/agency/listModel.jhtml" params:params httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
        
        NSLog(@"%@",result);
        
        [MyMbd hidenHudFromView:self.view];
        BOOL suc =[success boolValue];
        if (suc == 0 ) {
            UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [deleAlertView show];
            return;
        }else if(suc == 1){
            tableview.frame = tableview.frame = CGRectMake(WITH_FOR_40, classifyBtn.frame.origin.y+WITH_FOR_40,ScreenW-WITH_FOR_80 , 160);
            tableview.hidden = NO;
            classifyAry = result[@"commodity_models"][@"listItems"];
            btenclic=WitchBtnclicType;
            [tableview reloadData];
        }
    }failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
-(void)clickclassify:(UIButton *)sender{
    [classifyBtn setTitle:[NSString stringWithFormat:@"  %@",[classifyAry objectAtIndex:sender.tag -210][@"name"]] forState:UIControlStateNormal];
    [classifyBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    classifyStr =[classifyAry objectAtIndex:sender.tag -210][@"id_"];
    UIScrollView *scrollview = (UIScrollView *)[self.view viewWithTag:299];
    [scrollview removeFromSuperview];
}
//产品名称
-(void)loadmarkdata{
  
    classifyStr = nil;
    if(!nameStr){
        UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:@"请选择商品分类" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [deleAlertView show];
        return;
    }
    
    [MyMbd addHudToView:self.view];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:@"1" forKey:@"page"];
    [params setObject:nameid forKey:@"commodity_category_id_"];
    NSLog(@"--------+++++++++++%@",nameid);
    [params setObject:@"-1" forKey:@"name"];
    [params setObject:@"2" forKey:@"flag"];
    [WXDataService requestWithURL:@"mobile/commodity/listCommodityPage.jhtml" params:params httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
        
        
        NSLog(@"%@",result);
        
        [MyMbd hidenHudFromView:self.view];
        BOOL suc =[success boolValue];
        if (suc == 0 ) {
            UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [deleAlertView show];
            return;
        }else if(suc == 1){
            tableview.frame =tableview.frame = CGRectMake(WITH_FOR_40,markBtn.frame.origin.y +WITH_FOR_40,ScreenW-WITH_FOR_80 , 160);
            tableview.hidden=NO;
            markAry = result[@"commodities"][@"listItems"];
            btenclic=WitchBtnclicName;
            [tableview reloadData];
        }
    }failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    if(![markStr isEqualToString:@"请选择商品名称"]){
        [classifyBtn setTitle:@"请选择商品类型" forState:UIControlStateNormal];
        classifyStr = @"";
    }

}
-(void)clickmark:(UIButton *)sender{
    [markBtn setTitle:[NSString stringWithFormat:@"  %@",[markAry objectAtIndex:sender.tag -110][@"name"]] forState:UIControlStateNormal];
    [markBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    markStr =[markAry objectAtIndex:sender.tag -110][@"id_"];
    UIScrollView *scrollview = (UIScrollView *)[self.view viewWithTag:199];
    [scrollview removeFromSuperview];
}
-(void)clickname:(UIButton *)sender{
    [nameBtn setTitle:[NSString stringWithFormat:@"  %@",[nameAry objectAtIndex:sender.tag -10][@"name"]] forState:UIControlStateNormal];
    [nameBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    nameStr =[nameAry objectAtIndex:sender.tag -10][@"id_"];
    UIScrollView *scrollview = (UIScrollView *)[self.view viewWithTag:99];
    [scrollview removeFromSuperview];
}
#pragma mark - nav
-(void)loadNav{
    self.view.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    UIView *redview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 20)];
    redview.backgroundColor = DRINK_RED_COLOR;
    [self.view addSubview:redview];
    
    UILabel *redLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, ScreenW, 40)];
    redLab.backgroundColor = DRINK_RED_COLOR;
    redLab.text = @"上报库存";
    redLab.textColor = [UIColor whiteColor];
    redLab.textAlignment =  NSTextAlignmentCenter;
    redLab.font = [UIFont boldSystemFontOfSize:19];
    [self.view addSubview:redLab];
    
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20,44, 44)];
    backBtn.adjustsImageWhenHighlighted = NO;
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
}
-(void)clickBack{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    tableview.hidden = YES;
    [numTextField resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//
//    TypeBtn *butten = [[TypeBtn alloc]init ];
//    butten.frame =CGRectMake(0, 0, 375, 40);
//    
//    [butten addline:nameAry WithSection:section];
//    
//    [butten addTarget:self action:@selector(buttenClic:) forControlEvents:UIControlEventTouchUpInside];
//    
//    
//    return butten;
//
//
//}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (btenclic==WitchBtnclicCaguer) {
        return nameAry.count;
    }else if (btenclic ==WitchBtnclicName)
    {
        return markAry.count;
    }
    return classifyAry.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableview dequeueReusableCellWithIdentifier:@"cell3"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell3"];

    }
    cell.textLabel.textColor = [UIColor grayColor];
    UIView *bkbview = [[UIView alloc]init];
    bkbview.frame = CGRectMake(0, 0, 100, 100);
    bkbview.backgroundColor = rgb(235, 112, 52, 1);
    cell.selectedBackgroundView = bkbview;
    if (btenclic==WitchBtnclicCaguer) {
        cell.textLabel.text = nameAry[indexPath.row][@"name"];
    }else if (btenclic ==WitchBtnclicName)
    {
        cell.textLabel.text = markAry[indexPath.row][@"name"];
        
        NSLog(@"%@",stringmark);
    }else
    {
        cell.textLabel.text = classifyAry[indexPath.row][@"name"];

    }
    
    return cell;
}
#warning 选中cell之后让tableview隐藏
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableview.hidden = YES;
    
    
    
    
    
    
    if (btenclic== WitchBtnclicName) {
        
        [markBtn setTitle: markAry[indexPath.row][@"name"] forState:UIControlStateNormal];
        markStr = markAry[indexPath.row][@"name"];
        stringmark = markAry[indexPath.row][@"id_"];

    }else if (btenclic ==WitchBtnclicCaguer)
    {
        [nameBtn setTitle: nameAry[indexPath.row][@"name"] forState:UIControlStateNormal];
        nameStr = nameAry[indexPath.row][@"name"];
        nameid =nameAry[indexPath.row][@"id_"];
        NSLog(@"-----------------%@",nameid);

    }else
    {
        [classifyBtn setTitle: classifyAry[indexPath.row][@"name"] forState:UIControlStateNormal];
        classifyStr = classifyAry[indexPath.row][@"name"];
        productId = classifyAry[indexPath.row][@"id_"];
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if (buttonIndex ==0) {
        [self dismissViewControllerAnimated:YES completion:nil];
        self.block();
    }

}
//-(void)buttenClic:(TypeBtn *)btn
//{
//
//
//}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
